package dome2;

import java.util.Arrays;
import java.util.PriorityQueue;

public class TextHeap {
    public int[] elem;
    public int useSized;

    public TextHeap() {
        this.elem = new int[10];
    }

    public void intiElem(int[] array) {
        for (int i = 0; i < array.length; i++) {
            this.elem[i] = array[i];
            useSized++;
        }
    }

    public void creatHeap() {
        for (int parent = (useSized - 1 - 1) / 2; parent >= 0; parent--) {
            siftDown(parent, useSized);
        }
    }

    private void siftDown(int parent, int useSized) {
        int child = parent * 2 + 1;
        while (child < useSized) {
            if (child + 1 < useSized && elem[child] < elem[child + 1]) child++;
            if (elem[child] > elem[parent]) {
                swap(elem, child, parent);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    public void offer(int val) {
        if (isFull()) elem = Arrays.copyOf(elem, elem.length * 2);
        elem[useSized] = val;
        siftUp(useSized);
        useSized++;
    }

    private void siftUp(int child) {
        int parent = (child - 1) / 2;
        while (parent >= 0) {
            if (elem[parent] < elem[child]) {
                swap(elem, parent, child);
                child = parent;
                parent = (child - 1) / 2;
            } else {
                break;
            }
        }
    }

    private boolean isFull() {
        return useSized == elem.length;
    }

    private void swap(int[] elem, int i, int j) {
        int tmp = elem[i];
        elem[i] = elem[j];
        elem[j] = tmp;
    }

    public void poll() {
        if (isEmpty()) return;
        swap(elem, 0, useSized - 1);
        useSized--;
        siftDown(0, useSized);
    }

    public boolean isEmpty() {
        return useSized == 0;
    }

    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if (k == 0) return ret;
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        for (int i = 0; i < k; i++) {
            minHeap.offer(arr[i]);
        }
        for (int i = k; i < arr.length; i++) {
            int top = minHeap.peek();
            if (top < arr[i]) {
                minHeap.poll();
                minHeap.offer(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        return ret;
    }

    public int[] smallestK2(int[] arr, int k) {
        if (arr == null || k == 0) return new int[0];
        PriorityQueue<Integer> minHead = new PriorityQueue<>();
        for (int i = 0; i < arr.length; i++) {
            minHead.offer(arr[i]);
        }
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = minHead.poll();
        }
        return ret;
    }

    public void headSort() {
        int end = useSized - 1;
        while(end > 0) {
            swap(elem,0,end);
            siftDown(0,end);
            end--;
        }
    }
}
